package top.bulk.sql.util;

import org.springframework.util.ObjectUtils;

import java.text.DateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

/**
 * 工具类 用于处理 sql 的一些操作
 *
 * @author 散装java
 * @date 2024-07-31
 */
public class BulkSqlUtil {
    public static Integer getReturnRows(Object proceed) {
        if (proceed instanceof Collection) {
            return ((Collection<?>) proceed).size();
        }
        if (proceed instanceof Map) {
            return ((Map) proceed).size();
        }
        if (proceed instanceof Integer) {
            return (Integer) proceed;
        }
        return 0;
    }

    public static String getMethodName(String str, char separator) {
        if (str == null || str.length() == 0) {
            return str;
        }
        final int pos = str.lastIndexOf(separator);
        if (pos == -1) {
            return str;
        }
        final int secondPos = str.lastIndexOf(separator, pos - 1);
        if (secondPos == -1) {
            return str;
        }
        return str.substring(secondPos + 1);
    }

    public static String getParameterValue(Object object) {
        String value = "";
        if (object instanceof String) {
            value = "'" + object + "'";
        } else if (object instanceof Date) {
            DateFormat format = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA);
            value = "'" + format.format((Date) object) + "'";
        } else if (!ObjectUtils.isEmpty(object)) {
            value = object.toString();
        }
        return value;
    }
}
